<template>
  <!-- 工作日历类型表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="50%"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="工厂" prop="plantId" required>
        <el-select
          v-if="!props.isReadonly"
          @change="plantChange"
          v-model="formData.plantId"
          placeholder="请选择"
        >
          <el-option
            v-for="item in plants"
            :key="item.id"
            :label="item.organizationCode"
            :value="item.id"
          />
        </el-select>
        <span v-else> {{ formData.plantCode }}</span>
      </el-form-item>
      <el-row>
        <el-col :span="10.5">
          <el-form-item label="类型编号" prop="workCalendarTypeCode" required>
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.workCalendarTypeCode"
              placeholder="请输入工作日历类型编码"
              clearable
            />

            <span v-else>{{ formData.workCalendarTypeCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="10.5">
          <el-form-item label="类型名称" prop="workCalendarTypeName" required>
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.workCalendarTypeName"
              placeholder="请输入工作日历类型名称"
              clearable
            />

            <span v-else>{{ formData.workCalendarTypeName }}</span>
          </el-form-item></el-col
        >
      </el-row>

      <el-form-item label="是否启用" prop="enabled">
        <template v-if="!props.isReadonly">
          <el-radio-group v-model="formData.enabled">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </template>
        <span v-else>{{
          dictionaryStore.filterDictName("YesNo", formData.enabled)
        }}</span>
      </el-form-item>
      <el-row>
        <el-col :span="2"
          ><el-form-item label="工作日选择:" prop="workingDayMon">
            <el-checkbox
              v-if="!props.isReadonly"
              :disabled="false"
              v-model="formData.workingDayMon"
              :true-label="1"
              :false-label="0"
              >一</el-checkbox
            >
            <el-checkbox
              v-else
              :disabled="true"
              v-model="formData.workingDayMon"
              :true-label="1"
              :false-label="0"
              >{{ "一" }}</el-checkbox
            >
          </el-form-item>
        </el-col>
        <el-col :span="2">
          <el-form-item label="" prop="workingDayTue">
            <el-checkbox
              v-if="!props.isReadonly"
              v-model="formData.workingDayTue"
              :true-label="1"
              :false-label="0"
              >二</el-checkbox
            >
            <el-checkbox
              v-else
              :disabled="true"
              v-model="formData.workingDayTue"
              :true-label="1"
              :false-label="0"
              >{{ "二" }}</el-checkbox
            >
          </el-form-item></el-col
        >
        <el-col :span="2"
          ><el-form-item label="" prop="workingDayWed">
            <el-checkbox
              v-if="!props.isReadonly"
              v-model="formData.workingDayWed"
              :true-label="1"
              :false-label="0"
              >三</el-checkbox
            >
            <el-checkbox
              v-else
              :disabled="true"
              v-model="formData.workingDayWed"
              :true-label="1"
              :false-label="0"
              >{{ "三" }}</el-checkbox
            >
          </el-form-item>
        </el-col>
        <el-col :span="2">
          <el-form-item label="" prop="workingDayTur">
            <el-checkbox
              v-if="!props.isReadonly"
              v-model="formData.workingDayTur"
              :true-label="1"
              :false-label="0"
              >四</el-checkbox
            >
            <el-checkbox
              v-else
              :disabled="true"
              v-model="formData.workingDayTur"
              :true-label="1"
              :false-label="0"
              >{{ "四" }}</el-checkbox
            >
          </el-form-item></el-col
        >
        <el-col :span="2">
          <el-form-item label="" prop="workingDayFri">
            <el-checkbox
              v-if="!props.isReadonly"
              v-model="formData.workingDayFri"
              :true-label="1"
              :false-label="0"
              >五</el-checkbox
            >
            <el-checkbox
              v-else
              :disabled="true"
              v-model="formData.workingDayFri"
              :true-label="1"
              :false-label="0"
              >{{ "五" }}</el-checkbox
            >
          </el-form-item>
        </el-col>
        <el-col :span="2">
          <el-form-item label="" prop="workingDaySat">
            <el-checkbox
              v-if="!props.isReadonly"
              v-model="formData.workingDaySat"
              :true-label="1"
              :false-label="0"
              >六</el-checkbox
            >
            <el-checkbox
              v-else
              :disabled="true"
              v-model="formData.workingDaySat"
              :true-label="1"
              :false-label="0"
              >{{ "六" }}</el-checkbox
            >
          </el-form-item></el-col
        >
        <el-col :span="2">
          <el-form-item label="" prop="workingDaySon">
            <el-checkbox
              v-if="!props.isReadonly"
              v-model="formData.workingDaySon"
              :true-label="1"
              :false-label="0"
              >日</el-checkbox
            >
            <el-checkbox
              v-else
              :disabled="true"
              v-model="formData.workingDaySon"
              :true-label="1"
              :false-label="0"
              >{{ "日" }}</el-checkbox
            >
          </el-form-item></el-col
        >
      </el-row>

      <el-form-item
        v-if="props.isReadonly !== false"
        label="创建时间"
        prop="createTime"
      >
        <span>{{ formData.createTime }}</span>
      </el-form-item>
      <el-form-item
        v-if="props.isReadonly !== false"
        label="创建人"
        prop="createdUserName"
      >
        <span>{{ formData.createdUserName }}</span>
      </el-form-item>
      <el-form-item
        v-if="props.isReadonly !== false"
        label="修改时间"
        prop="lastUpdateTime"
      >
        <span>{{ formData.lastUpdateTime }}</span>
      </el-form-item>
      <el-form-item
        v-if="props.isReadonly !== false"
        label="修改人"
        prop="lastUpdatedUserName"
      >
        <span>{{ formData.lastUpdatedUserName }}</span>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MDMMDMWORKCALENDARTYPEADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

let plants = ref([]);
const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    getPlants();
    if (newVal === true) {
      setTimeout(() => {
        formRef.value.clearValidate();
      }, 50);
    }
  }
);

const rules = reactive({
  workCalendarTypeCode: [
    { required: true, message: "请输入工作日历类型编号", trigger: "blur" },
    {
      required: true,
      min: 1,
      max: 50,
      message: "长度在 1 到 50 个字符",
      trigger: "blur",
    },
  ],
  workCalendarTypeName: [
    { required: true, message: "请输入工作日历类型名称", trigger: "blur" },
    {
      required: true,
      min: 1,
      max: 50,
      message: "长度在 1 到 50 个字符",
      trigger: "blur",
    },
  ],
  plantCode: [
    { required: true, message: "请选择工厂", trigger: "change" },
    {
      required: true,
      min: 1,
      max: 50,
      message: "长度在 1 到 50 个字符",
      trigger: "blur",
    },
  ],
  plantId: [{ required: true, message: "请选择工厂", trigger: "change" }],
  workingDayMon: [],
  workingDayTue: [],
  workingDayWed: [],
  workingDayTur: [],
  workingDayFri: [],
  workingDaySat: [],
  workingDaySon: [],
  enabled: [],
  createTime: [],
  createdUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  createdUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  lastUpdateTime: [],
  lastUpdatedUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  lastUpdatedUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  version: "",
  workCalendarTypeCode: "",
  workCalendarTypeName: "",
  organizationId: "",
  organizationCode: "",
  plantId: "",
  plantCode: "",
  workingDayMon: 0,
  workingDayTue: 0,
  workingDayWed: 0,
  workingDayTur: 0,
  workingDayFri: 0,
  workingDaySat: 0,
  workingDaySon: 0,
  enabled: 1,
  deleted: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        version,
        workCalendarTypeCode,
        workCalendarTypeName,
        organizationId,
        organizationCode,
        plantId,
        plantCode,
        workingDayMon = 0,
        workingDayTue = 0,
        workingDayWed = 0,
        workingDayTur = 0,
        workingDayFri = 0,
        workingDaySat = 0,
        workingDaySon = 0,
        enabled,
        deleted,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      } = newVal;
      Object.assign(formData, {
        id,
        version,
        workCalendarTypeCode,
        workCalendarTypeName,
        organizationId,
        organizationCode,
        plantId,
        plantCode,
        workingDayMon,
        workingDayTue,
        workingDayWed,
        workingDayTur,
        workingDayFri,
        workingDaySat,
        workingDaySon,
        enabled,
        deleted,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.version = "";
  formData.workCalendarTypeCode = "";
  formData.workCalendarTypeName = "";
  formData.organizationId = "";
  formData.organizationCode = "";
  formData.plantId = "";
  formData.plantCode = "";
  formData.workingDayMon = 0;
  formData.workingDayTue = 0;
  formData.workingDayWed = 0;
  formData.workingDayTur = 0;
  formData.workingDayFri = 0;
  formData.workingDaySat = 0;
  formData.workingDaySon = 0;
  formData.enabled = 1;
  formData.deleted = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
}

const loading = ref(false);

/** 工作日历类型保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mdm/mdmworkcalendartype/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
        location.reload();
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}
async function getPlants() {
  const data = await request({
    url: "/system/sysOrganization/getList",
    method: "post",
    data: { organizationType: 3 },
  });
  plants.value = data.data;
}
function plantChange() {
  var plant = plants.value.find((item) => {
    return item.id === formData.plantId;
  });
  formData.plantCode = plant.organizationCode;
}
// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
